package com.orangelife.mobile.annoucement.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.curry.android.adapter.FatherAdapter;
import com.curry.android.net.NetworkUtil;
import com.curry.android.util.DecimalUtil;
import com.curry.android.util.ToastHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.annoucement.activity.AnnoucementListActivity;
import com.orangelife.mobile.annoucement.biz.MessageHttpBiz;
import com.common.jump.MessageJumper;
import com.orangelife.mobile.main.biz.JumpUtil;
import com.orangelife.mobile.util.DateUtil;

import java.util.List;
import java.util.Map;

/**
 * Created by YangWei
 * on 2015/11/11.
 */
public class AnnoucementListAdapter<T> extends FatherAdapter<T> {


    private List<Map<String, Object>> mList;
    private Context context;
    /*** 判断是小区公告的接口还是系统消息的接口 */
    private String mType;

    public AnnoucementListAdapter(Context context, List<Map<String, Object>> list, String type) {
        this.context = context;
        this.mList = list;
        this.mType = type;
    }

    @Override
    public BaseAdapter setList(List<T> list) {
        this.mList = (List<Map<String, Object>>) list;
        return AnnoucementListAdapter.this;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.item_lv_annoucement, null);
            holder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
            holder.tvContent = (TextView) convertView.findViewById(R.id.tvContent);
            holder.tvTime = (TextView) convertView.findViewById(R.id.tvTime);
            holder.llClick = (LinearLayout) convertView.findViewById(R.id.llClick);
            holder.ivGo = (ImageView) convertView.findViewById(R.id.iv_go);
            convertView.setTag(R.id.llClick,holder);
        } else {
            holder = (ViewHolder) convertView.getTag(R.id.llClick);
        }
        Map<String, Object> map = mList.get(position);
        String content = String.valueOf(map.get("content"));
        String readFlag = String.valueOf(map.get("readFlag"));
        holder.tvContent.setText(content);
        setHasRead(holder, readFlag);
        if (AnnoucementListActivity.TYPE_MESSAGE.equals(mType)) {
            String mesType = String.valueOf(map.get("mesType"));
            if ("7".equals(mesType)) {
                holder.ivGo.setVisibility(View.INVISIBLE);
            } else {
                holder.ivGo.setVisibility(View.VISIBLE);
            }
        }
        holder.tvTitle.setText(String.valueOf(map.get("title")));
        String tscreate = DecimalUtil.ScientificToString(String.valueOf(map.get("tscreate")));
        holder.tvTime.setText(DateUtil.getInstance().tranStringForDate(tscreate));
        holder.llClick.setTag(R.id.iv_go,map);
        holder.llClick.setOnClickListener(listener);
        return convertView;
    }

    private void setHasRead(ViewHolder holder, String readFlag) {
        if ("0".equals(readFlag)) {
            holder.tvTitle.setTextColor(context.getResources().getColor(R.color.DarkGray));
            holder.tvContent.setTextColor(context.getResources().getColor(R.color.lightGray));
        } else {
            holder.tvTitle.setTextColor(context.getResources().getColor(R.color.lightGray2));
            holder.tvContent.setTextColor(context.getResources().getColor(R.color.lineBackgroud));
        }
    }

    View.OnClickListener listener=new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            if (!NetworkUtil.isConnectedNet(context)) {
                ToastHelper.getInstance()._toast("网络不给力");
                return;
            }
            Map<String, Object> map= (Map<String, Object>) v.getTag(R.id.iv_go);
            if(map==null){
                return;
            }
            String readFlag = String.valueOf(map.get("readFlag"));
            String annoID = String.valueOf(map.get("mesID"));
            if (AnnoucementListActivity.TYPE_MESSAGE.equals(mType)) {/*如果是系统消息*/
                String url = String.valueOf(map.get("uri"));
                jumpToSystemMessage(url);
                if ("0".equals(readFlag)) {/*如果未读*/
                    String type = "2";/*此类型只返回errCode*/
                    String msgType = String.valueOf(map.get("msgType"));
                    if (msgType.equals("10")) {
                        type = "1";
                    }
                    requestHasRead(annoID,type);
                }
            } else {/*小区公告*/
                String url = String.valueOf(map.get("jumpuri"));
                jumpToAnnoDetail(url, annoID, readFlag);
            }
        }
    };

    /** 系统消息 */
    private void jumpToSystemMessage(String url) {
        JumpUtil.jumpActivity(context, url);
    }

    /** 公告详情 */
    private void jumpToAnnoDetail(String url, String annoID, String readFlag) {
        MessageJumper.jumpToAnnouncementDetail(context, url, annoID, false, readFlag);
    }

    private void requestHasRead(String annoID,String type) {
        MessageHttpBiz.requestDetail(annoID, type, null);
    }

    class ViewHolder {
        TextView tvTitle;
        TextView tvTime;
        TextView tvContent;
        LinearLayout llClick;
        ImageView ivGo;
    }
}
